package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.WheelView.TimePickerView;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.DepositOrder;
import com.haotunet.app.goldtreasure.model.StoreInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.net.UrlUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.TimeUtils;
import com.haotunet.app.goldtreasure.utils.UIHelper;

import java.util.Date;

public class StoreDepositReserveAct extends BaseActivity implements OnClickListener {
    private TextView mTitleTxt;
    private ImageView ivBack;
    private RelativeLayout rlLocate;
    private ImageView mStoreImage;
    private RelativeLayout rlReseverTime;
    private TextView tvReseverTime;
    private TextView mShopName, mShopAddress;
    private TextView mContactPerson, mContactNum;
    private Button btnResever;
    private ImageView btnCall;
    private CheckBox mProtocal;
    private TextView mProtocol;
    private DBOpenHelper helper;
    private String reseverTime;
    private final int SUBMIT_DEPOSIT_RESEVER_INFO_SUCCESS = 1;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case SUBMIT_DEPOSIT_RESEVER_INFO_SUCCESS:
                    DepositOrder orderModel = (DepositOrder) msg.obj;
                    ActivityUtils.gotoOrderSuccess(StoreDepositReserveAct.this, orderModel);
                    StoreDepositReserveAct.this.finish();
                default:
                    break;
            }
        }

    };

    private String shopId;
    private StoreInfo info;
    TimePickerView pvTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_deposit_reserve);
        tintManager.setStatusBarAlpha(0f);
        helper = new DBOpenHelper(StoreDepositReserveAct.this);
        initView();
        pvTime = new TimePickerView(this, TimePickerView.Type.ALL);
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        // 时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                tvReseverTime.setText(TimeUtils.getTime(date));
            }
        });

    }

    private void initView() {
        info = (StoreInfo) getIntent().getSerializableExtra("storeDetailInfo");
        shopId = getIntent().getStringExtra("shopId");
        mTitleTxt = (TextView) findViewById(R.id.title_txt);
        mTitleTxt.setText(getString(R.string.deposit_reservation));
        ivBack = (ImageView) findViewById(R.id.iv_left_arrow);
        rlLocate = (RelativeLayout) findViewById(R.id.locate_layout);
        mStoreImage = (ImageView) findViewById(R.id.shop_image);
        Glide.with(StoreDepositReserveAct.this).load(info.getImage().trim()).placeholder(R.drawable.placeholder_shop_big).error(R.drawable.placeholder_shop_big).into(mStoreImage);
        mShopName = (TextView) findViewById(R.id.shop_name);
        mShopName.setText(info.getShopName());
        mShopAddress = (TextView) findViewById(R.id.shop_address);
        mShopAddress.setText(info.getAddress());
        mContactNum = (TextView) findViewById(R.id.txt_contact_phone);
        mContactNum.setText(helper.getMobile());
        mContactPerson = (TextView) findViewById(R.id.txt_contact_person);
        mContactPerson.setText(helper.getRealName());
        btnCall = (ImageView) findViewById(R.id.iv_call);
        rlReseverTime = (RelativeLayout) findViewById(R.id.rl_resever_time_layout);
        tvReseverTime = (TextView) findViewById(R.id.tv_resever_time);
        mProtocal = (CheckBox) findViewById(R.id.cb_protocal);
        mProtocal.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mProtocal.setChecked(true);
                } else {
                    mProtocal.setChecked(false);
                }
            }
        });
        mProtocol = (TextView) findViewById(R.id.txt_protocol);
        btnResever = (Button) findViewById(R.id.btn_submit_reserve);
        ivBack.setOnClickListener(this);
        rlLocate.setOnClickListener(this);
        btnCall.setOnClickListener(this);
        rlReseverTime.setOnClickListener(this);
        mProtocol.setOnClickListener(this);
        btnResever.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left_arrow:
                finish();
                break;
            case R.id.locate_layout:
                if (info!=null) {
                    ActivityUtils.gotoShopPosition(StoreDepositReserveAct.this, info.getAddress(), info.getPoint().getX(),
                            info.getPoint().getY(), info.getShopName());
                }
                break;
            case R.id.iv_call:
                if (info!=null) {
                    UIHelper.showCallRemindDialog(StoreDepositReserveAct.this, info.getShopPhone());
                }
                break;
            case R.id.rl_resever_time_layout:
                pvTime.show();
                break;

            case R.id.txt_protocol:
                ActivityUtils.gotoDynamicDetail(StoreDepositReserveAct.this, UrlUtil.LEASE_PROTOCOL, 4);
                break;
            case R.id.btn_submit_reserve:
                if (!mProtocal.isChecked()) {
                    UIHelper.showToast(StoreDepositReserveAct.this, getString(R.string.please_agree_to_deposit_agreement));
                    return;
                }
                reseverTime = tvReseverTime.getText().toString().trim();
                if (TextUtils.isEmpty(reseverTime)) {
                    UIHelper.showToast(StoreDepositReserveAct.this, "请选择预约时间!");
                    return;
                }
                submitDepositReseverInfo();
                break;
            default:
                break;
        }
    }

    private void submitDepositReseverInfo() {
        ActionUtil actionUtil = new ActionUtil(StoreDepositReserveAct.this);
        actionUtil.submitDepositReseverInfo(shopId, reseverTime);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                DepositOrder orderModel = (DepositOrder) model;
                if (orderModel == null) {
                    UIHelper.showToast(StoreDepositReserveAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (orderModel.getErrorMsg() != null) {
                    if (orderModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(StoreDepositReserveAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(StoreDepositReserveAct.this, orderModel.getErrorMsg());
                    }
                } else {
                    Message msg = new Message();
                    msg.what = SUBMIT_DEPOSIT_RESEVER_INFO_SUCCESS;
                    msg.obj = orderModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

}
